add: leetcode - 460 lfu cache#81
Open
kwonyg wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
leetcode - 460 lfu cache(#76 )
LFU cache
운영체제 기준, 어떤 블록이 메모리에서 참조된 횟수를 관리한다. 캐시가 초과하면 가장 참조 빈도가 낮은 항목을 삭제한 후 새로운 항목을 집어넣는다. 참조 횟수가 똑같은 경우 가장 오래된 항목을 삭제한다.
시간복잡도 O(1)에 항목을 삭제하기 위해 이중 연결리스트 사용.
코드 참고: https://leetcode.com/problems/lfu-cache/discuss/446443/JavaScript-Solution
LRUCache
nodes: O(1) 시간에 노드에 접근 할 수 있도록 노드들을 저장하는 객체freqs: 이중 연결 리스트 인스턴스를 저장하는 공간,키: 접근 빈도 횟수, 값: 키만큼의 접근 빈도 횟수가진 노드들이 모인 이중 연결 리스트Node
freq: 얼마나 자주 접근했는지 판단DoublyLinkedList
삽입과 제거를 위해 사용된다. 삭제는 테일에서만 일어나고, 삽입은 헤드에서만 일어난다. 이유는 참조빈도가 같을 경우 생성된 지 가장 오래된 노드를 제거해야하는데, 새로 삽입하는 노드들을 헤드에만 삽입하게 되면 저절로 가장 오래된 노드는 테일쪽에 있기 때문이다.
put
freq의 이중 연결 리스트에 삽인 된다.leastFreq로 결정)get
leastFreq로 결정)동작 예시